package entity

import (
	"fmt"
	"time"

	"github.com/jinzhu/gorm"
	"shqsoft.cn/pasnj/base"
)

//IndiBxtz 保险台账
type IndiBxtz struct {
	ID      int        `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	XSZH    string     `gorm:"column:XSZH"`             //销售支行
	BXGS    string     `gorm:"column:BXGS"`             //保险公司
	TBDH    string     `gorm:"column:TBDH"`             //投保单号
	TBRXM   string     `gorm:"column:TBRXM"`            //投保人姓名
	TBRZJHM string     `gorm:"column:TBRZJHM"`          //投保人证件号码
	XZMC    string     `gorm:"column:XZMC"`             //险种名称
	XZ      string     `gorm:"column:XZ"`               //险种
	SQBF    float64    `gorm:"column:SQBF"`             //首期保费
	BXHTH   string     `gorm:"column:BXHTH"`            //保险合同号
	HTSXRQ  *time.Time `gorm:"column:HTSXRQ;type:date"` //合同生效日期
	HZQS    string     `gorm:"column:HZQS"`             //回执签收
	GH      string     `gorm:"column:GH"`               //工号
	TJR     string     `gorm:"column:TJR"`              //推荐人
	DJ      string     `gorm:"column:DJ"`               //趸交
	ISYT    string     `gorm:"column:ISYT"`             //是否犹退
	GMQD    string     `gorm:"column:GMQD"`             //购买渠道
	CCBX    string     `gorm:"column:CCBX"`             //财产保险
	LX      string     `gorm:"column:LX"`               //类型
	BZ      string     `gorm:"column:BZ"`               //备注
	ZSBL    float64    `gorm:"column:ZSBL"`             //中收比率
	ZS      float64    `gorm:"column:ZS"`               //中收
	TBRQ    *time.Time `gorm:"column:TBRQ;type:date"`   //投保日期
	EMID    int        `gorm:"column:EMID"`             //员工ID
}

//TableName 保险台账表名
func (IndiBxtz) TableName() string {
	return "T_INDI_BXTZ"
}

//BeforeSave 保存前的触发器
func (i *IndiBxtz) BeforeSave(tx *gorm.DB) (err error) {
	row := tx.Raw("select id from t_sysuser where sysuserid=?", i.GH).Row()
	var emid int
	row.Scan(&emid)
	if emid == 0 {
		err = fmt.Errorf("工号%s在系统中没有找到！", i.GH)
	}
	i.EMID = emid
	return nil
}

func IndiBxtzEF() base.EntityFunc {
	return func() interface{} {
		entity := IndiBxtz{}
		return &entity
	}
}
func IndiBxtzESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []IndiBxtz
		return &entities
	}
}

func init() {
	dao := base.NewDefaultDao(IndiBxtzEF(), IndiBxtzESF(), nil)
	filter := base.DefaultFilter{
		FilterColName: "EMID",
		TableName:     "",
		EmidColName:   "EMID",
		AcidColName:   "",
		WhereCause:    "",
	}
	dao.Fileter = &filter
	base.DaoPoolObj.Put("IndiBxtz", dao)
}
